package com.smarthome.api.mobile.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;

import com.smarthome.common.mobile.Service;
import com.smarthome.core.propertymanager.PropertyServiceFeedbackService;
import com.smarthome.domain.bean.ShAccountMessage;
import com.smarthome.domain.bean.ShServiceFeedback;
import com.smarthome.domain.mobile.MobileCodeEnum;
import com.smarthome.domain.mobile.MobileResponse;
import com.smarthome.domain.mobile.req.MobilePropertyServiceFeedbackRequest;

/**
 * 
* @ClassName: MobilePropertyServiceFeedbackService
* @Description: 移动端服务反馈service
* @author cyj
* @date 2015年10月14日 下午4:31:10
*
 */
@org.springframework.stereotype.Service("mobile.propertyServiceFeedback.service")
public class MobilePropertyServiceFeedbackService implements Service<MobileResponse<?>> {

	@Resource
	private PropertyServiceFeedbackService propertyServiceFeedbackService;
	
	
	/**
	 * 
	* getList
	* @Title: getList
	* @Description: 获取物业服务反馈列表
	* @param request
	* @param response
	* void
	* @throws
	* @author cyj
	* @date 2015年10月14日 下午5:09:39
	 */
	public void getList(MobilePropertyServiceFeedbackRequest request,MobileResponse<List<ShServiceFeedback>> response) {
		if(request.getServiceId() == null) {
			response.setMobileCodeObj(MobileCodeEnum.PROPERTY_SERVICE_FEEDBACK_NOT_SERVICE);
			return;
		}
		ShServiceFeedback queryServciceFeedback = new ShServiceFeedback();
		BeanUtils.copyProperties(request, queryServciceFeedback);
		queryServciceFeedback.setAccountId(null);
		queryServciceFeedback.setActive(1);
		queryServciceFeedback.setOrderField("gmtCreate");
		queryServciceFeedback.setOrderFieldType("asc");
		int count = propertyServiceFeedbackService.count(queryServciceFeedback);
		if(request.getStartIndex() != null && request.getStartIndex() >= count) {
			//response.setMobileCodeObj(MobileCodeEnum.PAGE_NOTSHOW);
			response.setData(new ArrayList<ShServiceFeedback>());
			return;
		}
		try {
			List<ShServiceFeedback> propertyServiceFeedbackList = propertyServiceFeedbackService.getPropertyServiceFeedbackList(queryServciceFeedback);
			for(ShServiceFeedback feedback:propertyServiceFeedbackList) {
				if(feedback.getAccountId().equals(request.getAccountId())) {
					feedback.setSource(2);
				}else {
					feedback.setSource(1);
				}
			}
			response.setData(propertyServiceFeedbackList);
		}catch(Exception e) {
			e.printStackTrace();
			response.setMobileCodeObj(MobileCodeEnum.SYSTEM_ERROR);
		}
	}
	
	/**
	 * 
	* add
	* @Title: add
	* @Description: 添加物业服务反馈
	* @param request
	* @param response
	* void
	* @throws
	* @author cyj
	* @date 2015年10月14日 下午5:44:52
	 */
	public void add(MobilePropertyServiceFeedbackRequest request,MobileResponse<?> response) {
		if(request.getServiceId() == null) {
			response.setMobileCodeObj(MobileCodeEnum.PROPERTY_SERVICE_FEEDBACK_NOT_SERVICE);
			return;
		}
		if(request.getAccountId() == null) {
			response.setMobileCodeObj(MobileCodeEnum.PROPERTY_SERVICES_FEEDBACK_NOT_ACCOUNT);
			return;
		}
		if(StringUtils.isBlank(request.getContent())) {
			response.setMobileCodeObj(MobileCodeEnum.PROPERTY_SERVICES_FEEDBACK_NOT_CONTENT);
			return;
		}
		ShServiceFeedback newServciceFeedback = new ShServiceFeedback();
		BeanUtils.copyProperties(request, newServciceFeedback);
		newServciceFeedback.setStatus(1);
		newServciceFeedback.setActive(1);
		newServciceFeedback.setGmtCreate(new Date());
		int result = 0;
		try {
			result = propertyServiceFeedbackService.insert(newServciceFeedback);
		}catch(Exception e) {
			e.printStackTrace();
			response.setMobileCodeObj(MobileCodeEnum.SYSTEM_ERROR);
			return;
		}
		if(result != 1) {
			response.setMobileCodeObj(MobileCodeEnum.PROPERTY_SERVICES_FEEDBACK_ADD_FAIL);
		}
	}
}
